package com.turnengine.client.local.location.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.location.bean.GridDetailsSerializer;
import com.turnengine.client.local.location.bean.IGridDetails;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The Get Grid Details List Data Serializer.
 */
public class GetGridDetailsListDataSerializer extends AbstractCommandDataSerializer<List<IGridDetails>> implements IGetGridDetailsListDataSerializer {

	/**
	 * Creates a new Get Grid Details List Data Serializer.
	 */
	public GetGridDetailsListDataSerializer() {
		super(GetGridDetailsList.COMMAND_ID);
	}

	@Override
	public List<IGridDetails> readReturnValue(IDataReader reader) throws IOException {
		List<IGridDetails> value = reader.readObjectCollection(new ArrayList<IGridDetails>(), true, false, new GridDetailsSerializer());
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, List<IGridDetails> value) throws IOException {
		writer.writeObjectCollection(value, true, false, new GridDetailsSerializer());
	}

	@Override
	public IExecutableCommand<List<IGridDetails>> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int[] coordinates = reader.readIntArray(false);

		// Create object
		GetGridDetailsList object = new GetGridDetailsList();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setCoordinates(coordinates);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<List<IGridDetails>> argument) throws IOException {
		GetGridDetailsList object = (GetGridDetailsList) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int[] coordinates = object.getCoordinates();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeIntArray(coordinates, false);
	}

	@Override
	public IExecutableCommandResponse<List<IGridDetails>> newResponse() {
		return new GetGridDetailsListResponse();
	}
}
